﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace CraigsCrawler
{
    public static class Parse
    {
        private static readonly XNamespace rssNamespace = @"http://purl.org/rss/1.0/";
        private static readonly XNamespace rssDcNamespace = @"http://purl.org/dc/elements/1.1/";
        private static readonly XName rssItem = rssNamespace + "item";
        private static readonly XName rssTitle = rssNamespace + "title";
        private static readonly XName rssLink = rssNamespace + "link";
        private static readonly XName rssDcDate = rssDcNamespace + "date";

        public static IEnumerable<Entry> RssForEntries(SearchStorage parent, string url, Stream content)
        {
            string city = Utilities.GetCity(url);

            XDocument doc = XDocument.Load(content);
            List<XElement> items = doc.Descendants(rssItem).ToList();

            foreach (XElement item in items)
            {
                string link = item.Element(rssLink).Value;
                string title = item.Element(rssTitle).Value;
                DateTime posted = DateTime.Parse(item.Element(rssDcDate).Value);

                Entry entry = new Entry(null)
                {
                    city = city,
                    entryState = EntryState.New,
                    link = link,
                    parent = parent,
                    posted = posted,
                    title = title,
                    thumbnail = null,
                    lastRetrieved = DateTime.UtcNow
                };

                yield return entry;
            }
        }

        public static string PageForThumb(Entry entry, string url, Stream content)
        {
            // Search for: <img src="http://images.craigslist.org/thumb/3Eb3Mc3Ja5E25F25Mcd3udd477325f9161358.jpg" alt="image 1">

            StreamReader reader = new StreamReader(content);
            string text = reader.ReadToEnd();

            Regex regex = new Regex(@"<img src=""(http://images.craigslist.org/thumb/\w+\.jpg)"" alt=""image \d+"">");
            MatchCollection matches = regex.Matches(text);
            if (matches.Count == 0)
            {
                return string.Empty;
            }

            return matches[0].Groups[1].Value;
        }

        public static bool PageForRemoved(Stream content)
        {
            // <div id="userbody">
            // <div class="removed">

            StreamReader reader = new StreamReader(content);
            string text = reader.ReadToEnd();
            Regex regex = new Regex(@"<div id=""userbody"">\s+<div class=""removed"">");
            return regex.IsMatch(text);
        }

        public static IEnumerable<string> LandingForCities(Stream content)
        {
            StreamReader reader = new StreamReader(content);
            string result = reader.ReadToEnd();

            Regex docTypeRemover = new Regex(@"<!DOCTYPE[^>]*>");
            result = docTypeRemover.Replace(result, "").Trim();

            Regex metaReplacer = new Regex(@"(<meta[^>]*[^>/])(>)");
            result = metaReplacer.Replace(result, @"$1/>");

            Regex linkReplacer = new Regex(@"(<link[^>]*[^>/])(>)");
            result = linkReplacer.Replace(result, @"$1 />");

            result = result.Replace("devon & cornwall", "devon and cornwall");
            result = result.Replace("&copy;", "copyright");
            result = result.Replace("for (i=0; i<data.length; i++)", "for some nonsense");
            result = result.Replace("popupContent += (data[i]['region']) ? '<br>' + data[i]['region'] : '';", "popupContent += (data[i]['region']) ? '<br/>' + data[i]['region'] : '';");

            MemoryStream stream = new MemoryStream();
            StreamWriter writer = new StreamWriter(stream);
            writer.Write(result);
            writer.Flush();
            stream.Position = 0;

            XDocument doc = XDocument.Load(stream);

            IEnumerable<XElement> divs = doc.Descendants("div").Where(e => e.Attributes("class").Where(a => a.Value.StartsWith("box_")).Any()).ToList();
            IEnumerable<XElement> lis = divs.Take(4).SelectMany(d => d.Descendants("li")).ToList();
            IEnumerable<string> links = lis.Select(l => l.Elements("a").First().Attribute("href").Value).ToList();

            return links.Select(Utilities.GetCity).Where(c => !String.IsNullOrWhiteSpace(c)).ToArray();
        }
    }
}
